package com.wf.mock.app.mina.app;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;

import com.google.common.base.Charsets;
import com.wf.mock.app.mina.ConnectorManager;
import com.wf.mock.app.mina.MessageIntentType;
import com.wf.mock.app.mina.dto.RawMessageDTO;
import com.wf.mock.app.mina.dto.SplitMsgPacketDTO;
import com.wf.mock.app.utils.MinaUtils;
import com.wf.mock.app.utils.TokenUtils;

public class MockGuodanClient {

	public static void main(String[] args) throws InterruptedException {
		IoConnector connector = ConnectorManager.getAppConnector();
		ConnectFuture connectFuture = connector.connect();

		connectFuture.awaitUninterruptibly();

		IoSession session = connectFuture.getSession();
		session.write(MessageIntentType.APPCONNECT + "{'token':'" + TokenUtils.generateToken("guodan@wafertest.com")
				+ "'}");

		for (int i = 0; i < 1; i++) {
			String msg = buildApp2PortalPrivateMsg(MinaUtils.generateUUID());
			RawMessageDTO create = RawMessageDTO.create(msg);

			MinaUtils.inject2ContentTime(create.getMsgBody());
			List<String> msgs = SplitMsgPacketDTO.generateMsgPacket(create.toString());
			for (String string : msgs) {
				System.out.println(string.getBytes(Charsets.UTF_8).length);
				session.write(string);
			}
			TimeUnit.SECONDS.sleep(1);
		}

		while (true) {
			TimeUnit.SECONDS.sleep(30);
		}
	}

	private static String buildApp2PortalPrivateMsg(String uuid) {
		StringBuilder content = new StringBuilder();

		for (int i = 0; i < 50000; i++) {
			content.append("o");
		}

		return MessageIntentType.APP2PORTAL
				+ "{'userId':'guodan','domain':'wafertest.com','operateType':'SENDMSG','markId':'" + uuid
				+ "','content':'test" + content.toString() + "','timeStamp':" + System.currentTimeMillis()
				+ ",'unreadSize':0,'msgType':10,'contentType':'TEXT_PLAIN','target':'guochao'}";
	}

}
